'use strict'

/**
 * 对象简单创建
 * @type {{}}
 */

var Person = {};//JSON
Person.name = 'Rahul Wu'; //定义属性
Person.age = 28;
Person['city'] = 'GZ';
Person.sayHello = function () { //定义函数
    console.log('my name is' + Person.name + 'and I am ' + Person.age + ' years old, I located at' + Person.city);
}
Person.sayHello();//执行函数


/**
 * function 即对象
 * @constructor
 */
function Person2() {

}
var person = new Person2();//定义一个function，如果有new关键字去"实例化",那么该function可以看作是一个类
person.name = 'Rahul Wu';
person.age = 28;
person.city = 'GZ';
person.sayHello = function () {
    console.log('my name is' + person.name + 'and I am ' + person.age + ' years old, I located at' + person.city);
}

person.sayHello();


/**
 * 构造器方式
 * @param name
 * @param age
 * @param city
 * @param func
 * @constructor
 */
function Person3(name, age, city) {
    this.name = name;
    this.age = age;
    this.city = city;
    this.sayHello = function () {
        console.log('my name is' + this.name + 'and I am ' + this.age + ' years old, I located at' + this.city);
    };
}

var person3 = new Person3('Rahul wu', 28, 'GZ');
person3.sayHello();

/**
 * 原型链方式
 * @constructor
 */
function Person4() {

}
Person4.prototype.name = 'Rahul wu';
Person4.prototype.age = 28;
Person4.prototype.city = 'GZ';
Person4.prototype.sayHello = function () {
    console.log('my name is' + this.name + 'and I am ' + this.age + ' years old, I located at' + this.city);
}
var person4 = new Person4();
person4.sayHello();


/**
 * 闭包创建
 */
var person5 = (function () {
    function Person() {
        this.name = 'Rahul wu';
        this.age = 28;
        this.city = 'GZ';
        this.sayHello = function () {
            console.log('my name is' + this.name + 'and I am ' + this.age + ' years old, I located at' + this.city);
        };
    }

    return new Person();
})();

person5.sayHello();

/**
 * 单例模式
 * @param fn
 * @returns {Function}
 */
var SingletonPerson = (function () {
    //单例方法
    function Singleton(args) {
        var args = args || {};
        this.name = 'Rahul wu'; //方法对外的属性，另外一种方式就是返回对象
        this.age = args.age || 28;
        this.city = args.city || 'GZ';
        this.sayHello = function () {
            console.log('my name is' + this.name + 'and I am ' + this.age + ' years old, I located at' + this.city);
        };
    }
    //单例实例
    var instance;
    //返回对象
    return {
        getInstance: function (args) {
            instance ? instance : instance = new Singleton(args);
            return instance;
        }
    };
})(); //直接执行该方法
var person6 = SingletonPerson.getInstance();
person6.sayHello();